package com.woniuxy01;

import java.io.*;

/**
 * @author 作者
 * @create 2025-06-10 14:16
 */
public class Test04 {

    public static void main(String[] args) throws IOException {
        String source = "d:\\aaa\\a.pdf";
        String target = "d:\\aaa\\b.pdf";
//        String source = "a.txt";
//        String target = "b.txt";

        copyFile4(source, target);


    }

    private static void copyFile4(String source, String target) throws IOException {
        //复制文件，一次读取一个字节
        long begin = System.currentTimeMillis();
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(source));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(target));
        int len = 0;
        byte[] bytes = new byte[1024];
        while ((len = bis.read(bytes)) != -1) {
            bos.write(bytes, 0, len);
        }
        bos.close();
        bis.close();
        long end = System.currentTimeMillis();
        System.out.println(end - begin);
        //22M 178ms
    }

    private static void copyFile3(String source, String target) throws IOException {
        //带缓冲区的字节流，一次读取一个字节
        long begin = System.currentTimeMillis();
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(source));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(target));
        int mybyte = 0;
        while ((mybyte = bis.read()) != -1) {
            bos.write(mybyte);
        }
        bos.close();
        bis.close();
        long end = System.currentTimeMillis();
        System.out.println(end - begin);
        //22M   120942ms
        //      732
    }


    private static void copyFile2(String source, String target) throws IOException {
        //复制文件，一次读取一个字节
        long begin = System.currentTimeMillis();
        FileInputStream fis = new FileInputStream(source);
        FileOutputStream fos = new FileOutputStream(target);
        int len = 0;
        byte[] bytes = new byte[2];
        while ((len = fis.read(bytes)) != -1) {
            fos.write(bytes, 0, len);
        }
        fos.close();
        fis.close();
        long end = System.currentTimeMillis();
        System.out.println(end - begin);
        //22M 178ms
    }
    private static void copyFile1(String source, String target) throws IOException {
        //复制文件，一次读取一个字节
        long begin = System.currentTimeMillis();
        FileInputStream fis = new FileInputStream(source);
        FileOutputStream fos = new FileOutputStream(target);
        int mybyte = 0;
        while ((mybyte = fis.read()) != -1) {
            fos.write(mybyte);
        }
        fos.close();
        fis.close();
        long end = System.currentTimeMillis();
        System.out.println(end - begin);
        //22M   120942ms
    }


}
